<?php
/**
 * @category   Epic
 * @package    Epic_Cache
 * @author     Epic Core Team <rukanhitokiri@gmail.com>
 */
class Epic_Cache_Content implements Epic_Cache_Interface
{
	/**
	 * Cache directory
	 * @var string
	 */
    protected $_cacheDir = CACHE_DIR;
    
    /**
     * How much time will keep the cache files in seconds
     * @var int
     */
    protected $_expireTime = 0;

    /**
     * Set time to keep the cache files
     *
     * @param int $seconds time to keep the cache files
     * @throws Epic_Cache_Exception cache directory is invalid
     */
    public function setExpireTime($seconds)
    {
    	if (!is_int($seconds))
    		throw new Epic_Cache_Exception('The specified expire time is invalid.');
    	$this->_expireTime = $seconds;
    }
    
    /**
     * Set cache directory
     * 
     * @param string $cacheDir cache directory
     * @throws Epic_Cache_Exception Cache directory is not exits
     * @throws Epic_Cache_Exception Cache directory must be a valid writeable directory
     */
    public function setCacheDir($cacheDir = '')
    {
    	if ($cacheDir !== '') {
    		if (!is_dir($cacheDir))
    			throw new Epic_Cache_Exception('Cache directory is not exits.');
    		if (!is_writable($cacheDir))
    			throw new Exception('Cache directory must be a valid writeable directory.');
    		$this->_cacheDir = $cacheDir;
    	}
    }
    
    /**
     * Get the cache file path
     *
     * @param string $file name file cache
     * @return string cache file path
     */
    protected function getCacheFile($file)
    {
    	return $this->_cacheDir . DS . strtolower($file);
    }
    
    /**
     * (non-PHPdoc)
     * 
     * @see Epic_Cache_Interface::exists()
     */
    public function exists($file)
    {
    	$cacheFile = $this->getCacheFile($file);
    	if (file_exists($cacheFile) && (filemtime($cacheFile) + $this->_expireTime) > time()) {
    		return true;
    	}
    	return false;
    }
   
	/**
	 * (non-PHPdoc)
	 * 
	 * @see Epic_Cache_Interface::set()
     * @throws Epic_Cache_Exception error saving data to the cache file
     * @return Epic_Cache_Content
	 */
    public function set($file, $data)
    {
        $cacheFile = $this->getCacheFile($file);
        if (!file_put_contents($cacheFile, $data)) {
            throw new Epic_Cache_Exception('Error saving data with the key ' . $file . ' to the cache file.');
        }
        return $this;
    }
   
	/**
	 * (non-PHPdoc)
	 * 
	 * @see Epic_Cache_Interface::get()
     * @throws Epic_Cache_Exception error reading data from the cache file
	 */
    public function get($file)
    {
        if ($this->exists($file)) {
	     	$cacheFile = $this->getCacheFile($file);
            if (!$data = file_get_contents($cacheFile)) {
                throw new Epic_Cache_Exception('Error reading data with the key ' . $file . ' from the cache file.');
            }
            return $data;
        }
        return null;
    }
   
	/**
	 * (non-PHPdoc)
	 * 
	 * @see Epic_Cache_Interface::delete()
     * @throws Epic_Cache_Exception Error deleting the file cache
     * @return boolean
	 */
    public function delete($file)
    {
        if ($this->exists($file)) {
            $cacheFile = $this->getCacheFile($file);
            if (!unlink($cacheFile))
                throw new Epic_Cache_Exception('Error deleting the file cache with key ' . $file);
            return true;
        }
        return false;
    }   
}